//
//  ZLMQVerCodeView.m
//  ZhangLu
//
//  Created by runone02 on 2018/11/19.
//  Copyright © 2018年 com.runone. All rights reserved.
//

#import "ZLMQVerCodeView.h"
#import "MQVerCodeImageView.h"
#import "CodeInputView.h"
@interface ZLMQVerCodeView()
@property (nonatomic, strong) MQVerCodeImageView *codeImageView;
/**输入框*/
@property (nonatomic,strong) CodeInputView *TF;
/**输入框字符串*/
@property (nonatomic,copy) NSString *backString;
/**图片字符串*/
@property (nonatomic,copy) NSString *imageCodeStr;
/**X按钮*/
@property (nonatomic,strong) UIButton *endButton;
/**提示*/
@property (nonatomic,strong) UILabel *wrongLabel;
/**输入框底部View*/
@property (nonatomic,strong) UIView *TFBGView;
@end
@implementation ZLMQVerCodeView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI {
    __weak __typeof(self)weakSelf = self;
    self.backgroundColor = RGBA(59, 59, 59, 0.6);
    
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.borderWidth = 1.0f;
    bgView.layer.borderColor = [UIColor whiteColor].CGColor;
    bgView.layer.cornerRadius = W_RATIO(10);
    bgView.clipsToBounds = YES;

    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(kTopBarHeight+30);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.bottom.mas_equalTo(self).with.offset(-100);
    }];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = @"请输入图形验证码";
    label.font = TITLE_FONT(32);
    label.textColor = [UIColor blackColor];
    [bgView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bgView).with.offset(20);
        make.left.mas_equalTo(bgView).with.offset(W_RATIO(30));
        make.height.mas_equalTo(10);
    }];
    
    _codeImageView = [[MQVerCodeImageView alloc] initWithFrame:CGRectMake(10, 60, SCREEN_WIDTH - 60, 120)];
    [bgView addSubview: _codeImageView];
    _codeImageView.isRotation = YES;
    _codeImageView.bolck = ^(NSString *imageCodeStr){//看情况是否需要
        NSLog(@"imageCodeStr = %@",imageCodeStr);
        weakSelf.imageCodeStr = imageCodeStr;
    };
    _codeImageView.isRotation = NO;
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [_codeImageView addGestureRecognizer:labelTapGestureRecognizer];
    _codeImageView.userInteractionEnabled = YES; 

    [_codeImageView freshVerCode];
    
    UIButton *refButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [refButton addTarget:self action:@selector(tapClick:) forControlEvents:UIControlEventTouchUpInside];
    [refButton setTitle:@"点击图形刷新" forState:UIControlStateNormal];
//    [refButton setImage:IMAGE(@"icon_notice") forState:UIControlStateNormal];
    refButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [refButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    refButton.titleLabel.font = TITLE_FONT(28);
    [bgView addSubview:refButton];
    [refButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.codeImageView.mas_bottom).with.offset(10);
        make.right.mas_equalTo(bgView).with.offset(-15);
        make.left.mas_equalTo(bgView);
    }];
    
    UILabel *hintLabel = [[UILabel alloc]init];
    hintLabel.text = @"请输入4位字母/数字";
    hintLabel.textColor = [UIColor lightGrayColor];
    hintLabel.textAlignment = NSTextAlignmentLeft;
    hintLabel.font = TITLE_FONT(28);
    [bgView addSubview:hintLabel];
    [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(refButton.mas_bottom).with.offset(20);
        make.left.mas_equalTo(bgView).with.offset(15);
        make.height.mas_equalTo(10);
    }];
    
    self.wrongLabel = [[UILabel alloc]init];
    self.wrongLabel.textColor = [UIColor orangeColor];
    self.wrongLabel.hidden = YES;
    self.wrongLabel.text = @"图形验证错误,请重新输入";
    self.wrongLabel.font = TITLE_FONT(28);
    [bgView addSubview:self.wrongLabel];
    [self.wrongLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(hintLabel.mas_bottom).with.offset(80);
        make.left.mas_equalTo(bgView).with.offset(15);
        make.height.mas_equalTo(10);
    }];
    
    self.TFBGView = bgView;
    [self loadTF];

    self.endButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.endButton addTarget:self action:@selector(endClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.endButton setImage:IMAGE(@"icon_close") forState:UIControlStateNormal];
    self.endButton.backgroundColor = [UIColor redColor];
    [self.endButton setTitle:@"X" forState:UIControlStateNormal];
    [self addSubview:self.endButton];
    
    [self.endButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(bgView.mas_bottom).with.offset(15);
    }];
}

- (void)loadTF {
    __weak __typeof(self)weakSelf = self;
    self.TF = [[CodeInputView alloc]initWithFrame:CGRectMake(10, 250, SCREEN_WIDTH - 60, 50) inputType:4 selectCodeBlock:^(NSString *code) {
        weakSelf.backString = code;
        if (code.length >= 4) {
            [weakSelf endButtonClick];
        }
        
    }];
     [self.TFBGView addSubview:self.TF];
}

- (void)endButtonClick{
//    __weak __typeof(self)weakSelf = self;

    if ([[self.backString lowercaseString] isEqualToString:[self.imageCodeStr lowercaseString]]) {
        if (_codeblock) {
            _codeblock(self.backString);
        }
//        [weakSelf endClick];
        [SVProgressHUD showSuccessWithStatus:@"正确"];
        
    }else{
        self.wrongLabel.hidden = NO;

    }
}


//点击刷新验证码
- (void)tapClick:(UITapGestureRecognizer*)tap{
    self.wrongLabel.hidden = YES;
    [_codeImageView freshVerCode];
}


- (void)endClick{
    
    [self removeFromSuperview];
}

@end
